# @time     :2021/12/15
# @Author   :Simple
# @FileName :Address

from qyex.api.base_class import BaseClass


class Address(BaseClass):
    def get_information(self, userid):
        """
        查询成员
        :param userid: str
        :return:
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/user/get"
        params = {
            "userid": userid
        }
        r = self.send("GET", url, params=params)
        return r

    def add_information(self, userid, name, mobile, department):
        """
        添加成员
        :param userid: str
        :param name: str
        :param mobile: str 11位数字手机号
        :param department: list
        :return:
        """
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/create"
        data = {
            "userid": userid,
            "name": name,
            "mobile": mobile,
            "department": department
        }
        # 使用json参数，可以自动把dict类型转成json类型并传参
        r = self.send("POST", url, json=data)
        # proxies = {"https": "https://127.0.0.1:8888"}
        # proxies设置http/https代理，verify是否开启ssl代理（此时报错SSLError）
        # r = requests.post(url, json=data, proxies=proxies, verify=False)
        return r

    def update_information(self, userid, name, mobile, department):
        """
        更新成员
        :param userid: str
        :param name: str
        :param mobile: str 11位数字手机号
        :param department: list
        :return:
        """
        url = f"https://qyapi.weixin.qq.com/cgi-bin/user/update"
        data = {
            "userid": userid,
            "name": name,
            "mobile": mobile,
            "department": department
        }
        r = self.send("POST", url, json=data)
        return r

    def delete_information(self, userid):
        """
        删除成员
        :param userid: str
        :return:
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete"
        params = {
            "userid": userid
        }
        r = self.send("GET", url, params=params)
        return r

    def get_departments(self, id=None):
        """
        查询部门
        :param id: 部门id，str,(optional)
        :return:
        """
        if id:
            url = f"https://qyapi.weixin.qq.com/cgi-bin/department/list?id={id}"
        else:
            url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"
        r = self.send("GET", url)
        return r
